package com.wkck.controller;

import com.wkck.domain.RegisterUser;
import com.wkck.service.RegisterUserService;
import com.wkck.utils.Code;
import com.wkck.utils.Msg;
import com.wkck.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/registers")
@Slf4j
public class RegisterController {

    @Autowired
    private RegisterUserService registerUserService;

    /**
     * 实现用户是否存在接口
     * @param username
     * @return
     */

    @GetMapping("/flagUser/{username}")
    public Result userRegister(@PathVariable String username){
        boolean data = registerUserService.existUser(username);
        Integer code = data ? Code.INEXISTENCE_USER_ERROR : Code.INEXISTENCE_USER_OK;
        String msg = data ? Msg.INEXISTENCE_USER_ERROR : Msg.INEXISTENCE_USER_OK;
        return new Result(code,data,msg);
    }

    /**
     * 实现用户的注册
     * @param registerUser
     * @return
     */

    @PostMapping("/saveUser")
    public Result saveUser(@RequestBody RegisterUser registerUser){
        boolean save = registerUserService.save(registerUser);
        Integer code = save ? Code.REGISTER_USER_OK : Code.REGISTER_USER_ERROR;
        String msg = save ? Msg.REGISTER_USER_OK : Msg.REGISTER_USER_ERROR;
        return new Result(code,save,msg);
    }

    /**
     * 判断用户名与手机号是否匹配
     * @param registerUser
     * @return
     */

    @PostMapping("/checkUser")
    public Result usernameMatchesPhoneNumber(@RequestBody RegisterUser registerUser){
        boolean flag = registerUserService.usernameMatchesPhoneNumber(registerUser);
        Integer code = flag ? Code.MATCHES_USER_OK : Code.MATCHES_USER_ERROR;
        String msg = flag ? Msg.MATCHES_USER_OK : Msg.MATCHES_USER_ERROR;
        return new Result(code,flag,msg);
    }


    /**
     * 根据用户名修改密码
     * @param registerUser
     * @return
     */

    @PutMapping("/upUser")
    public Result updateUser(@RequestBody RegisterUser registerUser){
        boolean flag = registerUserService.updateUser(registerUser);
        Integer code = flag ? Code.UPDATE_OK : Code.UPDATE_ERR;
        String msg = flag ? Msg.UPDATE_OK :Msg.UPDATE_ERR;
        return new Result(code,flag,msg);
    }
}
